using 高校员工餐饮信息管理系统.Models;

namespace 高校员工餐饮信息管理系统.Services
{
    /// <summary>
    /// 商户服务实现
    /// </summary>
    public class MerchantService : IMerchantService
    {
        private readonly ILogger<MerchantService> _logger;
        
        public MerchantService(ILogger<MerchantService> logger)
        {
            _logger = logger;
        }
        
        public async Task<Merchant?> GetMerchantByIdAsync(int merchantId)
        {
            _logger.LogInformation($"获取商户信息: {merchantId}");
            
            // TODO: 实际项目中应从数据库查询
            await Task.Delay(100);
            
            return new Merchant
            {
                Id = merchantId,
                Name = "食堂一楼餐厅",
                Code = "M001",
                ContactPerson = "李经理",
                ContactPhone = "13800138000",
                Status = "正常"
            };
        }
        
        public async Task<List<Merchant>> GetAllMerchantsAsync()
        {
            _logger.LogInformation("获取所有商户");
            
            // TODO: 实际项目中应从数据库查询
            await Task.Delay(100);
            
            return new List<Merchant>
            {
                new Merchant { Id = 1, Name = "食堂一楼餐厅", Code = "M001", Status = "正常" },
                new Merchant { Id = 2, Name = "食堂二楼餐厅", Code = "M002", Status = "正常" },
                new Merchant { Id = 3, Name = "教工餐厅", Code = "M003", Status = "正常" }
            };
        }
        
        public async Task<List<Order>> GetMerchantOrdersAsync(int merchantId, DateTime startDate, DateTime endDate)
        {
            _logger.LogInformation($"查询商户订单: {merchantId}, {startDate:yyyy-MM-dd} ~ {endDate:yyyy-MM-dd}");
            
            // TODO: 实际项目中应从数据库查询
            await Task.Delay(100);
            
            var orders = new List<Order>();
            var random = new Random();
            
            for (int i = 0; i < 10; i++)
            {
                orders.Add(new Order
                {
                    Id = i + 1,
                    UserId = random.Next(1, 100),
                    MerchantId = merchantId,
                    TotalAmount = random.Next(20, 50),
                    SubsidyAmount = 15,
                    SelfPayAmount = random.Next(5, 35),
                    OrderTime = startDate.AddDays(random.Next(0, (endDate - startDate).Days)),
                    Status = "已完成"
                });
            }
            
            return orders;
        }
        
        public async Task<byte[]> ExportMerchantSettlementDataAsync(int merchantId, DateTime startDate, DateTime endDate)
        {
            _logger.LogInformation($"导出商户结算数据: {merchantId}, {startDate:yyyy-MM-dd} ~ {endDate:yyyy-MM-dd}");
            
            // TODO: 实际项目中应使用EPPlus或NPOI生成Excel文件
            await Task.Delay(100);
            
            // 模拟返回CSV格式数据
            var csv = "订单号,用户ID,总金额,补贴金额,自费金额,订单时间,状态\n";
            var orders = await GetMerchantOrdersAsync(merchantId, startDate, endDate);
            
            foreach (var order in orders)
            {
                csv += $"{order.Id},{order.UserId},{order.TotalAmount},{order.SubsidyAmount},{order.SelfPayAmount},{order.OrderTime:yyyy-MM-dd HH:mm:ss},{order.Status}\n";
            }
            
            return System.Text.Encoding.UTF8.GetBytes(csv);
        }
    }
}

